//import java.awt.*;
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

//import java.lang.*;

public class Interface extends JFrame {

	private JPanel pannelButton = new JPanel();
	private JPanel pannel1 = new JPanel();
	private Bouton boutonPlay = new Bouton(new ImageIcon(getClass() .getResource("/Button-Play-icon.png")), new ImageIcon(getClass() .getResource("/Button-Play-icon-pressed.png")));
	private Bouton boutonStop = new Bouton(new ImageIcon(getClass() .getResource("/Button-Stop-icon.png")),new ImageIcon(getClass() .getResource("/Button-Stop-icon-pressed.png")));
	private Bouton boutonPause = new Bouton(new ImageIcon(getClass() .getResource("Button-Pause-icon.png")), new ImageIcon(getClass() .getResource("/Button-Pause-icon-pressed.png")));
	private Bouton boutonNext = new Bouton(new ImageIcon(getClass() .getResource("/Button-Last-icon.png")),new ImageIcon(getClass() .getResource("/Button-Last-icon-pressed.png")));
	private Bouton boutonPrec = new Bouton(new ImageIcon(getClass() .getResource("/Button-First-icon.png")), new ImageIcon(getClass() .getResource("/Button-First-icon-pressed.png")));
	private JSlider slide = new JSlider(SwingConstants.HORIZONTAL, 0, 100, 50);
	private client MonClient;
	private JLabel currentName = new JLabel("");
	private Thread T;
	private Interface InterfaceSave;

	public Interface(String titre) {
		super(titre);
		this.setLayout(new BorderLayout());
		MonClient = new client();
		this.setSize(650, 330);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		currentName.setFont(new Font(Font.SERIF, Font.BOLD, 18));
		currentName.setBorder(new TitledBorder(""));
		// ligne 1
		Box b1 = Box.createHorizontalBox();
		b1.add(boutonPlay);
		// ligne 2
		Box b2 = Box.createHorizontalBox();
		b2.add(boutonPrec);
		b2.add(boutonPause);
		b2.add(boutonNext);
		// ligne 3
		Box b3 = Box.createHorizontalBox();
		b3.add(boutonStop);
		// complet
		Box b4 = Box.createVerticalBox();
		b4.add(b1);
		b4.add(b2);
		b4.add(b3);
		Box b5 = Box.createHorizontalBox();
		b5.add(b4);
		b5.add(slide);
		// gestion du panel entier : text de la musique en cours en haut,
		// playlist au milieu, commandes en bas
		// .add(boutonQuitter);
		pannelButton.add(b5);
		slide.addChangeListener(new VolumeListener());
		boutonPlay.addActionListener(new PlayListener());
		boutonStop.addActionListener(new StopListener());
		boutonPause.addActionListener(new PauseListener());
		boutonNext.addActionListener(new NextListener());
		boutonPrec.addActionListener(new PrecListener());
		// gestio de la fermeture Fermeture
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new CloseListener());
		InterfaceSave = this;

		// mise en place finalle sur le panel
		this.getContentPane().add(pannelButton, BorderLayout.SOUTH);
		pannel1.add(currentName);
		this.add(pannel1);
		this.setVisible(true);
		T = new Thread(new Reception());
		T.start();
	}

	public void quitter() {
		try {
			// mettre la converion en UTF8 au cas ou pour utilisation sous
			// windaube

			String s1 = "stop\n";
			byte[] b = s1.getBytes("UTF8" /* encoding */);
			String s2 = new String(b, "UTF8");
			MonClient.envoyer(s2);
			Thread.sleep(100);
			s1 = "close\n";
			b = s1.getBytes("UTF8" /* encoding */);
			s2 = new String(b, "UTF8");
			MonClient.envoyer(s2);
			Thread.sleep(100);
			MonClient.close();
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Erreur fermeture Socket!");
		}
		try {
			System.exit(0);
		} catch (Exception ex) {
			JOptionPane.showMessageDialog(null, "Problème lors de la fermeture");
		}

	}

	public void AfficherNomChanson() {
		String encodingName = "UTF8";
		String s1, s2;
		try {
			s1 = "currentsong\n";
			byte[] b = s1.getBytes("UTF8" /* encoding */);
			s2 = new String(b, encodingName);
			MonClient.envoyer(s2);
			// reception et affichage
			String InfoChanson[] = MonClient.client_reception();
			currentName.setText(InfoChanson[1] + ":" + InfoChanson[0]);
		} catch (Exception e) {
		}
	}

	class PlayListener implements ActionListener {
		public void actionPerformed(ActionEvent arg) {
			String encodingName = "UTF8";
			String s1, s2;
			try {
				s1 = "play\n";
				byte[] b = s1.getBytes("UTF8" /* encoding */);
				s2 = new String(b, encodingName);
				MonClient.envoyer(s2);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"Erreur lors de l'envoie de la commande PLAY");
			}
		}
	}

	class StopListener implements ActionListener {
		public void actionPerformed(ActionEvent arg) {
			String encodingName = "UTF8";
			String s1, s2;
			try {
				s1 = "stop\n";
				byte[] b = s1.getBytes("UTF8" /* encoding */);
				s2 = new String(b, encodingName);
				MonClient.envoyer(s2);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"Erreur lors de l'envoie de la commande STOP");
			}
		}
	}

	class PauseListener implements ActionListener {
		public void actionPerformed(ActionEvent arg) {
			String encodingName = "UTF8";
			String s1, s2;
			try {
				s1 = "pause\n";
				byte[] b = s1.getBytes("UTF8" /* encoding */);
				s2 = new String(b, encodingName);
				MonClient.envoyer(s2);
				// System.out.println(s1 + " envoyé");
				// System.out.println("S2 = " + s2);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"Erreur lors de l'envoie de la commande PAUSE");
			}
		}
	}

	class NextListener implements ActionListener {
		public void actionPerformed(ActionEvent arg) {
			String encodingName = "UTF8";
			String s1, s2;
			try {
				s1 = "next\n";
				byte[] b = s1.getBytes("UTF8" /* encoding */);
				s2 = new String(b, encodingName);
				MonClient.envoyer(s2);
				// System.out.println(s1 + " envoyé");
				// System.out.println("S2 = " + s2);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"Erreur lors de l'envoie de la commande NEXT");
			}
		}
	}

	class PrecListener implements ActionListener {
		public void actionPerformed(ActionEvent arg) {
			String encodingName = "UTF8";
			String s1, s2;
			try {
				s1 = "prev\n";
				byte[] b = s1.getBytes("UTF8" /* encoding */);
				s2 = new String(b, encodingName);
				MonClient.envoyer(s2);
				// System.out.println(s1 + " envoyé");
				// System.out.println("S2 = " + s2);
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null,
						"Erreur lors de l'envoie de la commande PREV");
			}
		}
	}

	class VolumeListener implements ChangeListener {
		public void stateChanged(ChangeEvent e) {
			MonClient.envoyer(((JSlider) e.getSource()).getValue());
		}
	}

	class Reception extends Thread {

		public void run() {
			while (true) {
				AfficherNomChanson();
				try {
					T.sleep(600);
				} catch (InterruptedException e) {
					T.currentThread().interrupt();
					break;
				}
			}
		}
	}

	class CloseListener extends WindowAdapter {

		public void windowClosing(WindowEvent e) {
			// System.out.println("WindowsClosing bien appelé");
			T.interrupt();
			InterfaceSave.quitter();
		}
	}
}
